package cn.com.cifi.mars.schedled;

import cn.com.cifi.mars.bean.bo.SysLogBO;
import cn.com.cifi.mars.constant.*;
import cn.com.cifi.mars.entity.SysJob;
import cn.com.cifi.mars.service.*;
import cn.com.cifi.mars.util.LocalDateTimeUtils;
import cn.com.cifi.mars.util.WebUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;


/**
 * 回款计划版本及回款计划报表定时任务
 *
 * @Author: ztz
 * @Date: 2020/11/17 10:27
 */
@Slf4j
@Component
public class ReturnPlanTasks {

    @Autowired
    private DmInvestmentReturnService dmInvestmentReturnService;
    @Autowired
    private SysJobService sysJobService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private SysLogService sysLogService;
    @Autowired
    private ReturnPlanProgressBaseService returnPlanProgressBaseService;

    /**
     * 定时生成能力版回款计划
     */
    @Scheduled(cron = "0 0 7 * * ?")
    public void calculateRealityCoefficient() {
        boolean appointIP = commonService.isAppointIP();
        if (!appointIP) {
            return;
        }
        SysJob job = sysJobService.selectJobByCode(Constants.ZERO_THIRTY_ONE);
        LocalDateTime start = null;
        LocalDateTime end = null;
        if (null != job) {
            Integer jobId = job.getId();
            int count = sysJobService.lockJobById(jobId);
            if (count == 1) try {
                start = LocalDateTime.now();
                //获取当前时间的yearMonth
                String yearMonth = LocalDateTimeUtils.getLocalMonth(LocalDateTime.now());
                dmInvestmentReturnService.refreshAbilityReturnPlan(null, yearMonth);
                saveLog("定时任务", "定时生成能力版回款计划", "定时生成能力版回款计划");
                end = LocalDateTime.now();
            } catch (Exception e) {
                log.error("context:" + e);
            } finally {
                sysJobService.resetJob(job, start, end);
            }
            else {
                sysJobService.resetStatus(job);
            }
        }
    }


    /**
     * <p>
     * 定时刷新投资回款分析表
     */
    @Scheduled(cron = "0 0 8 * * ?")
    public void calculateInvestCoefficient() {
        boolean appointIP = commonService.isAppointIP();
        if (!appointIP) {
            return;
        }
        SysJob job = sysJobService.selectJobByCode(Constants.ZERO_THIRTY_TWO);
        LocalDateTime start = null;
        LocalDateTime end = null;
        if (null != job) {
            Integer jobId = job.getId();
            int count = sysJobService.lockJobById(jobId);
            if (count == 1) {
                try {
                    start = LocalDateTime.now();
                    //获取当前时间的yearMonth
                    String yearMonth = LocalDateTimeUtils.getLocalMonth(LocalDateTime.now());
                    dmInvestmentReturnService.refreshDmInvestmentReturn(null, yearMonth);
                    saveLog("定时任务", "刷新投资回款分析表", "刷新投资回款分析表");
                    end = LocalDateTime.now();
                } catch (Exception e) {
                    log.error("context:" + e);
                } finally {
                    sysJobService.resetJob(job, start, end);
                }
            } else {
                sysJobService.resetStatus(job);
            }
        }
    }

    /**
     * 刷新符合条件的回款计划列表
     */
    @Scheduled(cron = "0 0 0/3 * * ?")
    public void returnPlanProgressTask() {
        boolean appointIP = commonService.isAppointIP();
        if (!appointIP) {
            return;
        }
        SysJob job = sysJobService.selectJobByCode(Constants.ZERO_THIRTY_SIX);
        LocalDateTime start = null;
        LocalDateTime end = null;
        if (null != job) {
            Integer jobId = job.getId();
            int count = sysJobService.lockJobById(jobId);
            if (count == 1) {
                try {
                    start = LocalDateTime.now();
                    returnPlanProgressBaseService.refresh();
                    saveLog("定时任务", "刷新回款计划列表", "刷新回款计划列表");
                    end = LocalDateTime.now();
                } catch (Exception e) {
                    log.error("###### errorMsg: ", e);
                    saveLog("定时任务", "刷新回款计划列表", e.toString());
                } finally {
                    sysJobService.resetJob(job, start, end);
                }
            } else {
                sysJobService.resetStatus(job);
            }
        }
    }

    //日志
    private boolean saveLog(String module, String title, String content) {
        SysLogBO bo = new SysLogBO();
        bo.setEvent(LogEventEnum.BROWSE);
        bo.setSource(LogSrcEnum.PC);
        String hostAddr = WebUtils.getLocalIP();
        bo.setIp(hostAddr);
        bo.setModule(module);
        bo.setTitle(title);
        bo.setContent(content);
        bo.setType(LogTypeEnum.ZERO.getKey());
        bo.setIsSuccess(IsSuccessEnum.SUCCESS.getKey());
        return sysLogService.add(bo, Constants.SYS);
    }

}
